package com.qlm.tongji.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.qlm.tongji.service.IStatisticService;
import com.qlm.tools.DateUtils;
import com.qlm.view.EchartDataView;

public class StatisticServiceImpl implements IStatisticService {

	@Override
	public EchartDataView getScanEDV(String startDate, String endDate) {
		EchartDataView edv = new EchartDataView();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		Date sDate = new Date();
		Date eDate = new Date();
		try {
			sDate = sdf.parse(startDate);
			eDate = sdf.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		int days = getDays(sDate, eDate);
		String[] dates = getXAxisData(days, sDate, eDate);

		edv.setxAxisData(dates);// 完成x轴数据的添加
		edv.setSeriesData(getSeriesByDate(days, dates, startDate, endDate));// 完成y轴数据的添加

		return edv;
	}

	/**
	 * 获取时间间隔天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	private int getDays(Date startDate, Date endDate) {
		int day = (int) ((endDate.getTime() - startDate.getTime()) / (86400000) + 1);
		return day;
	}

	/**
	 * 获取x轴数据 时间数组
	 * 
	 * @param days
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	private String[] getXAxisData(int days, Date startDate, Date endDate) {

		String[] dates = new String[days]; // 日期列表 x列数据

		Date dateAdd = null;
		for (int i = 0; i < days; i++) {
			dateAdd = DateUtils.addDate(startDate, Calendar.DAY_OF_MONTH, i);
			String dateStr = DateUtils.dateToString(dateAdd,
					DateUtils.PATTEN_YMD);
			dates[i] = dateStr;
		}
		return dates;
	}

	/**
	 * 获取y轴数据 对应日期的扫码数据量
	 * 
	 * @param days
	 * @param dates
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	private Integer[] getSeriesByDate(int days, String[] dates, String sDate,
			String eDate) {
		Integer[] counts = new Integer[days];
		for (int i = 0; i < days; i++) {
			counts[i] = 0;
		}
		String sql = " select DATE(scan_time) as scan_time,count(1) AS scanCount from scan_record"
				+ " where TO_DAYS(scan_time)>=TO_DAYS('"
				+ sDate
				+ "') and  TO_DAYS(scan_time)<=TO_DAYS('"
				+ eDate
				+ "')  GROUP BY TO_DAYS(scan_time);";
		List<Record> list = Db.use("tongji").find(sql);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Record record = list.get(i);
				Long scanCount = record.getLong("scanCount");
				Date scanTime = record.getDate("scan_time");
				String scan_time = DateUtils.dateToString(scanTime,
						DateUtils.PATTEN_YMD);
				for (int k = 0; k < days; k++) {
					if (dates[k].equals(scan_time)) {
						counts[k] = scanCount.intValue();
						break;
					}
				}
			}
		}

		return counts;
	}

	@Override
	public EchartDataView getScanEDVOneDay() {
		EchartDataView edv = new EchartDataView();
		int hour = new Date().getHours()+1;
		String[] hours = new String[hour]; // 日期列表 x列数据
		for (int i = 0; i < hour; i++) {
			hours[i] = i + "";
		}
		edv.setxAxisData(hours);// 完成x轴数据的添加
		edv.setSeriesData(getSeriesByHour(hour, hours));// 完成y轴数据的添加

		return edv;
	}

	private Integer[] getSeriesByHour(int hour, String[] hours) {
		Integer[] counts = new Integer[hour];
		for (int i = 0; i < hour; i++) {
			counts[i] = 0;
		}
		String sql = "select HOUR(scan_time) as scan_time,count(*) AS scanCount from scan_record "
				+ "where DATE( NOW() ) = DATE(scan_time) and HOUR(scan_time)<="
				+ hour + " GROUP BY HOUR(scan_time)";
		List<Record> list = Db.use("tongji").find(sql);
		for (int i = 0,len=list.size(); i< len; i++) {
			Record record = list.get(i);
			Long scanCount = record.getLong("scanCount");
			Integer scanTime = record.getInt("scan_time");
			for(int k=0;k<hour;k++){
				if(hours[k].equals(scanTime+"")){
					counts[k] = scanCount.intValue();
					break;
				}					
			}
		}
		return counts;
	}

	@Override
	public EchartDataView getHbEDV(String startDate, String endDate) {
		EchartDataView edv = new EchartDataView();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		Date sDate = new Date();
		Date eDate = new Date();
		try {
			sDate = sdf.parse(startDate);
			eDate = sdf.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		int days = getDays(sDate, eDate);
		String[] dates = getXAxisData(days, sDate, eDate);

		edv.setxAxisData(dates);// 完成x轴数据的添加
		edv.setSeriesData(getHbSeriesByDate(days, dates, startDate, endDate));// 完成y轴数据的添加

		return edv;
	}

	/**
	 * 获取日红包统计y轴数据
	 * @param days
	 * @param dates
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	private Object[] getHbSeriesByDate(int days, String[] dates,
			String sDate, String eDate) {
		Double[] counts = new Double[days];
		for (int i = 0; i < days; i++) {
			counts[i] = 0.0;
		}
		String sql = " select DATE(send_time) as scan_time,sum(amount) AS totalAmount from hb_record"
				+ " where TO_DAYS(send_time)>=TO_DAYS('"
				+ sDate
				+ "') and  TO_DAYS(send_time)<=TO_DAYS('"
				+ eDate
				+ "')  GROUP BY TO_DAYS(send_time);";
		List<Record> list = Db.use("tongji").find(sql);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Record record = list.get(i);
				BigDecimal scanCount = record.getBigDecimal("totalAmount");
				Date scanTime = record.getDate("scan_time");
				String scan_time = DateUtils.dateToString(scanTime,
						DateUtils.PATTEN_YMD);
				for (int k = 0; k < days; k++) {
					if (dates[k].equals(scan_time)) {
						counts[k] = scanCount.divide(new BigDecimal(100)).doubleValue();
						break;
					}
				}
			}
		}

		return counts;
	}

	@Override
	public EchartDataView getHbEDVOneDay() {
		EchartDataView edv = new EchartDataView();
		int hour = new Date().getHours()+1;
		String[] hours = new String[hour]; // 日期列表 x列数据
		for (int i = 0; i < hour; i++) {
			hours[i] = i + "";
		}
		edv.setxAxisData(hours);// 完成x轴数据的添加
		edv.setSeriesData(getHbSeriesByHour(hour, hours));// 完成y轴数据的添加

		return edv;
	}

	/**
	 * 获取红包实时统计y轴数据
	 * @param hour
	 * @param hours
	 * @return
	 */
	private Object[] getHbSeriesByHour(int hour, String[] hours) {
		Double[] counts = new Double[hour];
		for (int i = 0; i < hour; i++) {
			counts[i] = 0.0;
		}
		String sql = "select HOUR(send_time) as scan_time,sum(amount) AS scanCount from hb_record "
				+ "where DATE( NOW() ) = DATE(send_time) and HOUR(send_time)<="
				+ hour + " GROUP BY HOUR(send_time)";
		List<Record> list = Db.use("tongji").find(sql);
		for (int i = 0,len=list.size(); i< len; i++) {
			Record record = list.get(i);
			BigDecimal scanCount = record.getBigDecimal("scanCount");
			Integer scanTime = record.getInt("scan_time");
			for(int k=0;k<hour;k++){
				if(hours[k].equals(scanTime+"")){
					counts[k] = scanCount.divide(new BigDecimal(100)).doubleValue();
					break;
				}					
			}
		}
		return counts;
	}
}
